package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextWatcher;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    List<String> data;
    List<String> dataBak;
    String searchTxt ="";
    MyAdapter m;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String txt="第一章：重生叶家|第二章：宗门崛起|第三章：天梦古地|第四章：历练南卓域|第五章：潜龙榜|第六章：血魔战场|第七章：迷雾禁区|第八章：不死祭坛" +
                "第九章：武道茶会|第十章：人魔家族|第十一章：天武域见闻|第十二章：我为剑宗";
        String[] txts=txt.split("\\|");
        data= Arrays.asList(txts);
        dataBak=data;

        EditText et=findViewById(R.id.search);
        ListView lv=findViewById(R.id.lv);
        m=new MyAdapter();
        lv.setAdapter(m);

        et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                searchTxt=s.toString();
                String[] ss=searchTxt.split(" ");
                List<String> res=new ArrayList<>();
                for (String t:dataBak){
                    boolean found=true;
                    for (String x:ss){
                        if (x.length()==0||x.isEmpty()){
                            continue;
                        }
                        if (!t.contains(x)){
                            found=false;
                            break;
                        }
                    }
                    if (found){
                        res.add(t);
                    }
                }
                data=res;
                m.notifyDataSetChanged();

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }
    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView==null){
                convertView=getLayoutInflater().inflate(R.layout.cell,null);
            }
            String txt=data.get(position);
            TextView tv=convertView.findViewById(R.id.title);
            SpannableStringBuilder builder=new SpannableStringBuilder(txt);
            String[] key=searchTxt.split(" ");
            for (String k:key){
                int idx=txt.indexOf(k);
                ForegroundColorSpan span=new ForegroundColorSpan(Color.BLUE);
                builder.setSpan(span,idx,idx+k.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
            }
            tv.setText(builder);
            return convertView;
        }
    }
    }

